1 // ----------------------------------------------------------------------------
2 // <copyright file=
"Extensions.cs" company="Exit Games GmbH">
3 // PhotonNetwork Framework
for Unity - Copyright (C) 2011 Exit Games GmbH
4 // </copyright>
5 // <summary>
6 // Provides some helpful methods and extensions
for Hashtables, etc.
7 // </summary>
8 // <author>developer@exitgames.com</author>
9 // ----------------------------------------------------------------------------

10
11 using
System.Collections;
12 using
UnityEngine;
13 using
Hashtable = ExitGames.Client.Photon.Hashtable;
14 using
SupportClass = ExitGames.Client.Photon.SupportClass;
15
16
17 ///
<summary>
18 ///
This static class defines some useful extension methods for several existing classes (e.g. Vector3, float and others).
19 ///
</summary>
20 public
static class Extensions
21 {
22     
public static PhotonView[] GetPhotonViewsInChildren(this UnityEngine.GameObject go)
23     {
24         
return go.GetComponentsInChildren<PhotonView>(true) as PhotonView[];
25     }
26
27     
public static PhotonView GetPhotonView(this UnityEngine.GameObject go)
28     {
29         
return go.GetComponent<PhotonView>() as PhotonView;
30     }

31
32     ///
<summary>compares the squared magnitude of target - second to given float value</summary>
33     
public static bool AlmostEquals(this Vector3 target, Vector3 second, float sqrMagnitudePrecision)
34     {
35         
return (target - second).sqrMagnitude < sqrMagnitudePrecision; // TODO: inline vector methods to optimize?
36     }

37
38     ///
<summary>compares the squared magnitude of target - second to given float value</summary>
39     
public static bool AlmostEquals(this Vector2 target, Vector2 second, float sqrMagnitudePrecision)
40     {
41         
return (target - second).sqrMagnitude < sqrMagnitudePrecision; // TODO: inline vector methods to optimize?
42     }

43
44     ///
<summary>compares the angle between target and second to given float value</summary>
45     
public static bool AlmostEquals(this Quaternion target, Quaternion second, float maxAngle)
46     {
47         
return Quaternion.Angle(target, second) < maxAngle;
48     }

49
50     ///
<summary>compares two floats and returns true of their difference is less than floatDiff</summary>
51     
public static bool AlmostEquals(this float target, float second, float floatDiff)
52     {
53         
return Mathf.Abs(target - second) < floatDiff;
54     }

55
56     ///
<summary>
57     ///
Merges all keys from addHash into the target. Adds new keys and updates the values of existing keys in target.
58     ///
</summary>
59     ///
<param name="target">The IDictionary to update.</param>
60     ///
<param name="addHash">The IDictionary containing data to merge into target.</param>
61     
public static void Merge(this IDictionary target, IDictionary addHash)
62     {
63         
if (addHash == null || target.Equals(addHash))
64         {
65             
return;
66         }
67
68         
foreach (object key in addHash.Keys)
69         {
70             target[key] = addHash[key];
71         }
72     }

73
74     ///
<summary>
75     ///
Merges keys of type string to target Hashtable.
76     ///
</summary>
77     ///
<remarks>
78     ///
Does not remove keys from target (so non-string keys CAN be in target if they were before).
79     ///
</remarks>
80     ///
<param name="target">The target IDicitionary passed in plus all string-typed keys from the addHash.</param>
81     ///
<param name="addHash">A IDictionary that should be merged partly into target to update it.</param>
82     
public static void MergeStringKeys(this IDictionary target, IDictionary addHash)
83     {
84         
if (addHash == null || target.Equals(addHash))
85         {
86             
return;
87         }
88
89         
foreach (object key in addHash.Keys)
90         {
91             
// only merge keys of type string
92             
if (key is string)
93             {
94                 target[key] = addHash[key];
95             }
96         }
97     }

98
99     ///
<summary>
100     ///
Returns a string-representation of the IDictionary's content, inlcuding type-information.
101     ///
Note: This might turn out a "heavy-duty" call if used frequently but it's usfuly to debug Dictionary or Hashtable content.
102     ///
</summary>
103     ///
<param name="origin">Some Dictionary or Hashtable.</param>
104     ///
<returns>String of the content of the IDictionary.</returns>
105     
public static string ToStringFull(this IDictionary origin)
106     {
107         
return SupportClass.DictionaryToString(origin, false);
108     }

109
110     ///
<summary>
111     ///
This method copies all string-typed keys of the original into a new Hashtable.
112     ///
</summary>
113     ///
<remarks>
114     ///
Does not recurse (!) into hashes that might be values in the root-hash.
115     ///
This does not modify the original.
116     ///
</remarks>
117     ///
<param name="original">The original IDictonary to get string-typed keys from.</param>
118     ///
<returns>New Hashtable containing only string-typed keys of the original.</returns>
119     
public static Hashtable StripToStringKeys(this IDictionary original)
120     {
121         Hashtable target =
new Hashtable();
122         
foreach (DictionaryEntry pair in original)
123         {
124             
if (pair.Key is string)
125             {
126                 target[pair.Key] = pair.Value;
127             }
128         }
129
130         
return target;
131     }

132
133     ///
<summary>
134     ///
This removes all key-value pairs that have a null-reference as value.
135     ///
Photon properties are removed by setting their value to null.
136     ///
Changes the original passed IDictionary!
137     ///
</summary>
138     ///
<param name="original">The IDictionary to strip of keys with null-values.</param>
139     
public static void StripKeysWithNullValues(this IDictionary original)
140     {
141         
object[] keys = new object[original.Count];
142         
//original.Keys.CopyTo(keys, 0);
143         
int i = 0;
144         
foreach (object k in original.Keys)
145         {
146             keys[i++] = k;
147         }
148
149         
for (int index = 0; index < keys.Length; index++)
150         {
151             
var key = keys[index];
152             
if (original[key] == null)
153             {
154                 original.Remove(key);
155             }
156         }
157     }

158
159     ///
<summary>
160     ///
Checks if a particular integer value is in an int-array.
161     ///
</summary>
162     ///
<remarks>This might be useful to look up if a particular actorNumber is in the list of players of a room.</remarks>
163     ///
<param name="target">The array of ints to check.</param>
164     ///
<param name="nr">The number to lookup in target.</param>
165     ///
<returns>True if nr was found in target.</returns>
166     
public static bool Contains(this int[] target, int nr)
167     {
168         
if (target == null)
169         {
170             
return false;
171         }
172
173         
for (int index = 0; index < target.Length; index++)
174         {
175             
if (target[index] == nr)
176             {
177                 
return true;
178             }
179         }
180
181         
return false;
182     }
183 }

184
185
186 ///
<summary>Small number of extension methods that make it easier for PUN to work cross-Unity-versions.</summary>
187 public
static class GameObjectExtensions
188 {

189     ///
<summary>Unity-version-independent replacement for active GO property.</summary>
190     ///
<returns>Unity 3.5: active. Any newer Unity: activeInHierarchy.</returns>
191     
public static bool GetActive(this GameObject target)
192     {
193         #
if UNITY_3_5
194         
return target.active;
195         #
else
196         
return target.activeInHierarchy;
197         #endif
198     }
199
200     #
if UNITY_3_5
201     ///
<summary>Unity-version-independent setter for active and SetActive().</summary>
202     
public static void SetActive(this GameObject target, bool value)
203     {
204         target.active =
value;
205     }
206     #endif
207 }


----------------------------------------------------------------------------

PhotonNetwork Framework for Unity - Copyright (C) 2011 Exit Games GmbH

Provides some helpful methods and extensions for Hashtables, etc.

developer@exitgames.com

----------------------------------------------------------------------------

This static class defines some useful extension methods for several existing classes (e.g. Vector3, float and others).

compares the squared magnitude of target - second to given float value

return (target - second).sqrMagnitude < sqrMagnitudePrecision; TODO: inline vector methods to optimize?

compares the squared magnitude of target - second to given float value

return (target - second).sqrMagnitude < sqrMagnitudePrecision; TODO: inline vector methods to optimize?

compares the angle between target and second to given float value

compares two floats and returns true of their difference is less than floatDiff

Merges all keys from addHash into the target. Adds new keys and updates the values of existing keys in target.

The IDictionary to update.

The IDictionary containing data to merge into target.

Merges keys of type string to target Hashtable.

Does not remove keys from target (so non-string keys CAN be in target if they were before).

The target IDicitionary passed in plus all string-typed keys from the addHash.

A IDictionary that should be merged partly into target to update it.

only merge keys of type string

Returns a string-representation of the IDictionary's content, inlcuding type-information.

Note: This might turn out a "heavy-duty" call if used frequently but it's usfuly to debug Dictionary or Hashtable content.

Some Dictionary or Hashtable.

String of the content of the IDictionary.

This method copies all string-typed keys of the original into a new Hashtable.

Does not recurse (!) into hashes that might be values in the root-hash.

This does not modify the original.

The original IDictonary to get string-typed keys from.

New Hashtable containing only string-typed keys of the original.

This removes all key-value pairs that have a null-reference as value.

Photon properties are removed by setting their value to null.

Changes the original passed IDictionary!

The IDictionary to strip of keys with null-values.

original.Keys.CopyTo(keys, 0);

Checks if a particular integer value is in an int-array.

This might be useful to look up if a particular actorNumber is in the list of players of a room.

The array of ints to check.

The number to lookup in target.

True if nr was found in target.

Small number of extension methods that make it easier for PUN to work cross-Unity-versions.

Unity-version-independent replacement for active GO property.

Unity 3.5: active. Any newer Unity: activeInHierarchy.

Unity-version-independent setter for active and SetActive().




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.462 lượt xem

Gõ tìm kiếm nhanh...